home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 42 / wind4.prf < prev    next >
Text File  |  1986-07-17  |  19KB  |  328 lines

  1. .!****************************************************************************
  2. .! 
  3. .! ANTIC PUBLISHING INC., COPYRIGHT 1985.  REPRINTED BY PERMISSION.
  4. .!
  5. .! ** Professional GEM ** by Tim Oren
  6. .!
  7. .! Proff File by ST enthusiasts at
  8. .! Case Western Reserve University
  9. .! Cleveland, Ohio
  10. .! uucp : decvax!cwruecmp!bammi
  11. .! csnet: bammi@case
  12. .! arpa : bammi%case@csnet-relay
  13. .! compuserve: 71515,155
  14. .!
  15. .!****************************************************************************
  16. .!
  17. .!
  18. .!****************************************************************************
  19. .!
  20. .!            Begin Part 4
  21. .!
  22. .!****************************************************************************
  23. .!
  24. .PART IV Resource Structure
  25. .PP
  26. Welcome to the fourth installment of ST PRO GEM.  We are about to delve into
  27. the mysteries of GEM resource structure, and then use this knowledge to create
  28. some useful utilities for handling dialogs.  As with the past columns, there is
  29. once again a download file.  You will find it under the name GEMCL4.C in the
  30. ATARI 16-bit Forum (GO PCS-58).
  31. .PP
  32. The first and largest part of the download contains a C image of a sample
  33. resource file.  To create this listing, I used the GEM Resource  Construction
  34. Set to create a dummy resource with three dialogs including examples of all
  35. object types, then enabled the C output option and saved  the resource.  If you
  36. have access to a copy of RCS, I suggest that you  create your own listing in
  37. order to get a feel for the results.  Then, using  either listing as a roadmap
  38. to the resource, you can follow along as  we enter...
  39. .SH A MAZE OF TWISTY LITTLE PASSAGES.
  40. While a GEM resource is loaded as a block of binary information, it
  41. is actually composed of a number of different data structures.  These
  42. structures are linked together in a rather tangled hierarchy.  Our
  43. first job is to map this linkage system. 
  44. .PP
  45. The topmost structure in a resource file is the resource header. This is an
  46. array of words containing the size and offset within the resource of the other
  47. structures which follow.  This information is used by  GEM during the resource
  48. load process, and you should never need to access it. (The resource header does
  49. not appear in the C output file; it is generated by the RSCREATE utility if the
  50. C file is used to recreate the resource.)
  51. .PP
  52. The next structure of interest is the tree index.  This is an array of long
  53. pointers, each of which addresses the beginning of an object tree. Again, you
  54. wouldn't normally access this structure directly.  The GEM  rsrc_gaddr call uses
  55. it when finding trees' addresses.  This structure is called "rs_trindex" in the
  56. C output.
  57. .PP
  58. If you look at the contents of rs_trindex you will notice that the values are
  59. integers, instead of the pointers I described.  What has happened is that RCS
  60. has converted the pointers to indices into the object array. (If you actually
  61. used the C file to recreate the resource file, then the  pointers would be
  62. regenerated by RSCREATE.)
  63. .PP
  64. Now you can follow the link from rs_trindex to the objects stored in
  65. rs_object.  Take (for instance) the second entry in rs_trindex and count down
  66. that many lines in rs_object.  The following line (object) should start with a
  67. -1.  This indicates that it is the root object of a tree.  The following objects
  68. down to the next root belong to that tree. We'll pass over the details of
  69. inter-object linkage for now, leaving it for a later column.
  70. .PP
  71. There are a number of different fields in an object, but right now we'll
  72. concentrate on two of them: OB_TYPE and OB_SPEC.  The OB_TYPE is the field which
  73. contains mnemonics like G_STRING and G_BOX indicating the type of the object.
  74. The OB_SPEC is the only field in each object which  is a LONG - you can tell it
  75. by the L after the number.
  76. .PP
  77. What's in OB_SPEC depends on the object type, so we need to talk about what
  78. kinds of objects are available, what you might use them for, and finally how
  79. they use the OB_SPEC field.
  80. .PP
  81. The box type objects are G_BOX, G_IBOX, and G_BOXCHAR.  A G_BOX is an opaque
  82. rectangle, with an optional border.  It's used to create a solid patch of color
  83. or pattern on which to place other objects.  For instance, the background of a
  84. dialog is a G_BOX.
  85. .PP
  86. A G_IBOX is a hollow box which has only a border.  (If the border has no
  87. thickness, then the box is "invisible", hence the name.)  The favorite use for
  88. IBOXes is to hold radio buttons.  There is also one neat trick you can play with
  89. an IBOX.  If you have more than one object (say an image and a string) which you
  90. would like to have selected all at once, you can insert them in a dialog, then
  91. cover them with an IBOX. Since the box is transparent, they will show through.
  92. If you now make the box selectable, clicking on it will highlight the whole area
  93. at once!
  94. .PP
  95. The G_BOXCHAR is just like a G_BOX, except that a single character is drawn
  96. in its center.  They are mostly used as "control points": the FULLER, CLOSER,
  97. SIZER, and arrows in GEM windows are BOXCHARs, as are  the components of the
  98. color selection gadgets in the RCS.
  99. .PP
  100. The OB_SPEC for box type objects is a packed bit array.  Its various fields
  101. contain the background color and pattern, the border thickness and color, and
  102. the optional character and its color.
  103. .PP
  104. The string type objects are G_STRING, G_BUTTON, and G_TITLE. G_STRINGs (in
  105. addition to being a bad pun) are for setting up static explanatory text within
  106. dialogs.  The characters are always written in the "system font": full size,
  107. black, with no special effects.
  108. .PP
  109. We have already discussed many of the uses of G_BUTTONs.  They add a border
  110. around the text.  The thickness of a G_BUTTON's border is determined by what
  111. flags are set for the object.  All buttons start out with a border thickness of
  112. one pixel.  One pixel is added if the EXIT attribute is set, and one more is
  113. added if the DEFAULT attribute is set.
  114. .PP
  115. The G_TITLE type is a specially formatted text string used only in the title
  116. bar of menus.  This type is needed to make sure that the menus redraw correctly.
  117. The Resource Construction Set automatically handles inserting G_TITLEs, so you
  118. will seldom use them directly.
  119. .PP
  120. In a resource, the OB_SPEC for all string objects is a long pointer to a null
  121. terminated ASCII string.  The string data in the C file is shown in the BYTE
  122. array rs_strings.  Again you will notice that  the OB_SPECs in the C file have
  123. been converted to indices into rs_string. To find the string which matches the
  124. object, take the value of OB_SPEC and count down that many lines in rs_strings.
  125. The next line is the correct string.
  126. .PP
  127. The formatted text object types are G_TEXT, G_BOXTEXT, G_FTEXT, and
  128. G_FBOXTEXT.  G_TEXTs are a lot like strings, except that you can specify a
  129. color, different sizes, and a positioning rule for the text.   Since they
  130. require more memory than G_STRINGs, G_TEXTs should be used  sparingly to draw
  131. attention to important information within a dialog.   G_TEXTs are also useful
  132. for automatic centering of dialog text which is  changed at run-time.  I will
  133. describe this technique in detail later on.
  134. .PP
  135. The G_BOXTEXT type adds a solid background and border to the G_TEXT type.
  136. These objects are occasionally used in place of G_BUTTONs when their color will
  137. draw attention to an important object.
  138. .PP
  139. The G_FTEXT object is an editable text field.  You are able to specify a
  140. constant "template" of characters, a validation field for those characters which
  141. are to be typed in, and an initial value for the input characters.  You may also
  142. select color, size, and positioning rule for G_FTEXTs.  We'll discuss text
  143. editing at length below.
  144. .PP
  145. The G_FBOXTEXT object, as you might suspect, is the same as G_FTEXT with the
  146. addition of background and border.  This type is seldom used: the extra
  147. appearance details distract attention from the text being edited.
  148. .PP
  149. The OB_SPEC for a formatted text object is a pointer to yet another type of
  150. structure: a TEDINFO.  In the C file, you will find these in rs_tedinfo.  Take
  151. the OB_SPEC value from each text type object and count down that many entries in
  152. rs_tedinfo, finding the matching TEDINFO on the next line.  Each contains
  153. pointers to ASCII strings for the template, validation, and initialization.  You
  154. can find these strings in rs_strings, just as above.
  155. .PP
  156. There are also fields for the optional background and border details, and for
  157. the length of the template and text.  As we will see when discussing  editing,
  158. the most important TEDINFO fields are the TE_PTEXT pointer to  initialized text
  159. and the TE_TXTLEN field which gives its length.
  160. .PP
  161. The G_IMAGE object type is the only one of its kind.  A G_IMAGE is a
  162. monochrome bit image.  For examples, see the images within the various GEM alert
  163. boxes.  Note that monochrome does not necessarily mean black.  The image may be
  164. any color, but all parts of it are the SAME color.  G_IMAGEs are used as visual
  165. cues in dialogs.  They are seldom used as selectable items because their entire
  166. rectangle is inverted when they are clicked.  This effect is seldom visually
  167. pleasing, particularly if the image is colored.
  168. .PP
  169. G_IMAGE objects have an OB_SPEC which is a pointer to a further structure
  170. type: the BITBLK.  By now, you should guess that you will find it in the C file
  171. in the array rs_bitblk.  The BITBLK contains fields describing the height and
  172. width of the image in pixels, its color,nd it also contains a long pointer to
  173. the actual bits which make up the image.  In the C file, the images are encoded
  174. as hexadecimal words and stored in arrays named IMAG0, IMAG1, and so on.
  175. .PP
  176. The last type of object is the G_ICON.  Like the G_IMAGE, the G_ICON is a bit
  177. image, but it adds a mask array which selects what portions of the image will be
  178. drawn, as well as an explanatory text field.  A G_ICON may also specify
  179. different colors for its "foreground" pixels (the ones that are normally black),
  180. and its "background" pixels (which are normally white).
  181. .PP
  182. The pictures which you see in Desktop windows are G_ICONs, and so are the
  183. disks and trashcan on the desktop surface.  With the latter you will notice the
  184. effects of the mask.  The desktop shows through right up to the edge of the
  185. G_ICON, and only the icon itself (not a rectangle) is inverted when a disk is
  186. selected.
  187. .PP
  188. The OB_SPEC of an icon points to another structure called an ICONBLK.  It is
  189. shown in the C file as rs_iconblk.  The ICONBLK contains long pointers to its
  190. foreground bit array, to the mask bit array, and to the ASCII string of
  191. explanatory text.  It also has the foreground and background colors as well as
  192. the location of the text area from the  upper left of the icon.  The most common
  193. use of G_ICONs and ICONBLKs  is not in dialogs, instead they are used frequently
  194. in trees which are build at run-time, such as Desktop windows.  In a future
  195. article, we will return to a discussion of building such "on-the-fly" trees with
  196. G_ICONs.
  197. .PP
  198. Now, let's recap the hierarchy of resource structures:  The highest level
  199. structures are the resource header, and then the tree index.  The tree index
  200. points to the beginning of each object tree. The objects making up the tree are
  201. of several types, and depending on that type, they may contain pointers to ASCII
  202. strings, or to TEDINFO, ICONBLK, or BITBLK structures.  TEDINFOs contain further
  203. pointers to strings; BITBLKs have pointers to bit images; and ICONBLKs have
  204. both.
  205. .SH PUTTING IT TO WORK
  206. The most common situations requiring you to understand
  207. resource structures involve the use of text and editable text objects in
  208. dialogs.  We'll look at two such techniques.
  209. .PP
  210. Often an application requires two or more dialogs which are very similar
  211. except for one or two title lines.  In this circumstance, you can save a good
  212. deal of resource space by building only one dialog, and changing the title at
  213. run time.
  214. .PP
  215. It is easy to go wrong with this practice, however, because the obvious
  216. tactic of using a G_STRING and writing over its text at run time can go wrong.
  217. The first problem is that you must know in advance the longest title to be used,
  218. and put a string that long into the resource. If you don't you will damage other
  219. objects in the resource as you copy in characters.  The other problem is that a
  220. G_STRING is always drawn at the same place in a dialog.  If the length of the
  221. title changes from time to time, the dialog will have an unbalanced and sloppy
  222. appearance.
  223. .PP
  224. A better way to do this is to exploit the G_TEXT object type, and the TEDINFO
  225. structure.  The set_text() routine in the download shows how.  The parameters
  226. provided are the tree address, the object number, and the 32-bit address of the
  227. string to be substituted.  For this to work, the object referenced should be
  228. defined as a G_TEXT type object.   Additionally, the Centered text type should
  229. be chosen, and the object should have been "stretched" so that it fills the
  230. dialog box from side to side.
  231. .PP
  232. In the code, the first action is to get the OB_SPEC from the object which was
  233. referenced.  Since we know that the object is a G_TEXT, the OB_SPEC must point
  234. to a TEDINFO.  We need to change two fields in the TEDINFO.  The TE_PTEXT field
  235. is the pointer to the actual string to be displayed; we replace it with the
  236. address of our new string. The TE_TXTLEN field is loaded with the new string's
  237. length.  Since the Centered attribute was specified for the object, changing the
  238. TE_TXTLEN will cause the string to be correctly positioned in the middle of the
  239. dialog!
  240. .PP
  241. Editing text also requires working with the TEDINFO structure. One way of
  242. doing this is shown in the download.  The object to be used (EDITOBJ) is assumed
  243. to be a G_FTEXT or G_FBOXTEXT.  Since we will replace the initialized text at
  244. run time, that field may be left empty when building the object in the RCS.
  245. .PP
  246. The basic trick of this code is to point the TEDINFO's TE_PTEXT at a string
  247. which is defined in your code's local stack.  The advantages of this technique
  248. are that you save resource space, save static data by putting the string in
  249. reusable stack memory, and automatically create a scratch string which may be
  250. discarded if the dialog is cancelled.
  251. .PP
  252. The text string shown is arbitrarily 41 characters long.  You should give
  253. yours a length equal to the number of blanks in the object's template field plus
  254. one.  Note that the code is shown as a segment,  rather than a subroutine.  This
  255. is required because the text string must be allocated within the context of
  256. dialog handling routine itself,  rather than a routine which it calls!
  257. .PP
  258. After the tree address is found, the code proceeds to find the TEDINFO and
  259. modify its TE_PTEXT as described above.  However, the length which is inserted
  260. into TE_TXTLEN must be the maximum string length,  including the null!
  261. .PP
  262. The final line of code inserts a null into the first character of the
  263. uninitialized string.  This will produce an empty editing field when the dialog
  264. is displayed.  If there is an existing value for  the object, you
  265. should instead use strcpy() to move it into text[]. Once the dialog is
  266. complete, you should check its final status as described in the last
  267. article.  If an "OK" button was clicked, you will then use strcpy() to
  268. move the value in text[] back to its static location.
  269. .PP
  270. Although I prefer this method of handling editable text, another method
  271. deserves mention also.  This procedure allocates a full length text string of
  272. blanks when creating the editable object in the RCS. At  run-time, the TE_PTEXT
  273. link is followed to find this string's location in  the resource, and any
  274. pre-existing value is copied in. After the dialog is run, the resulting value is
  275. copied back out if the dialog completed successfully.
  276. .PP
  277. Note that in both editing techniques a copy of the current string value is
  278. kept within the application's data area.  Threading the resource whenever you
  279. need to check a string's value is extremely wasteful.
  280. .PP
  281. One final note on editable text objects:  GEM's editor uses the commercial at
  282. sign '@' as a "meta-character".  If it is the first byte of the initialized
  283. text, then the field is displayed blank no matter what follows.  This can be
  284. useful, but is sometimes confusing when a user in all innocence enters an @ and
  285. has his text disappear the next time the dialog is drawn!
  286. .SH LETTERS, WE GET LETTERS
  287. The Feedback section on ANTIC ST ONLINE is now
  288. functional and is producing a gratifying volume of response. A number of
  289. requests were made for topics such as ST hardware and ST BASIC which are beyond
  290. the intended scope of this column.  These have been referred to ANTIC's
  291. editorial staff for action.
  292. .PP
  293. So many good GEM questions were received that I will devote part of the next
  294. column to answering several of general interest.  Also, your  requests have
  295. resulting in scheduling future columns on VDI text output  and on the principles
  296. (or mythology) of designing GEM application interfaces. Finally, a tip of the
  297. hat to the anonymous reader who suggested including the actual definitions of
  298. all macro symbols, so that those without the appropriate H files can follow
  299. along.  As a result of this suggestion, the definitions for this column and the
  300. previous three are included at the end of the download.  Future articles will
  301. continue this practice.
  302. .SH STRAW POLL!
  303. I'd like to make a practice of using the Feedback to get your
  304. opinions on the column's format.  As a first trial, I'd like to know your
  305. feelings about my use of "portability macros" in the sample code.  These macros,
  306. LLGET for example, are used for compatibility between 68K GEM systems like the
  307. ST, and Intel based systems like the IBM PC.  This may be important to many
  308. developers. On the other hand, omitting them results in more natural looking C
  309. code.  For instance, in the download you will find a second version of
  310. set_text() as  described above, but without the portability macros.  So, I would
  311. like to know if you think we should  (A) Keep the macros - portability is
  312. important to serious developers, (B) Get rid of them - who cares about Intel
  313. chips anyway, or (C) Who cares? I'll tally the votes in two weeks and announce
  314. the results here.
  315. .SH STAY TUNED!
  316. As well as answers to feedback questions, the next column will
  317. discuss how GEM objects are linked to form trees, and how to use AES calls and
  318. your own code to manipulate them for fun and profit.  In the following
  319. installment, we'll look at the VDI raster operations (also known as "blit"
  320. functions).
  321. .!
  322. .!
  323. .!*****************************************************************************
  324. .!*                                          *
  325. .!*                End Part 4                      *
  326. .!*                                          *
  327. .!*****************************************************************************
  328.